package com.ohos.apollo_hos.loading;

import com.ohos.apollo_hos.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

/**
 * 等待状态效果
 */
public class LoadingUtil {
    public static final long WAITING_TIME = 2000L;
    private static AnimatorProperty animatorProperty;
    private static AnimatorProperty imageAnimatorProperty;

    public static void showDoadling(AbilitySlice slice) {
        slice.setUIContent(ResourceTable.Layout_loading_main);
        Image loading = (Image) slice.findComponentById(ResourceTable.Id_img_loading);

        loading.setBindStateChangedListener(new Component.BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                animatorProperty = loading.createAnimatorProperty();
                animatorProperty.rotate(360).setDuration(1000).setLoopedCount(100).start();
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {

            }
        });
    }

    public static void hideDiading(AbilitySlice slice, int layout) {
        if (animatorProperty != null) {
            animatorProperty.stop();
            animatorProperty.reset();
        }
        slice.setUIContent(layout);

    }

    public static void showImageDoadling(Image imgLoading) {
        imgLoading.setVisibility(Component.VISIBLE);
        imageAnimatorProperty = imgLoading.createAnimatorProperty();
        imageAnimatorProperty.rotate(360).setDuration(1000).setLoopedCount(100).start();
    }

    public static void hideImageDiading(Image loading) {
        if (imageAnimatorProperty != null) {
            imageAnimatorProperty.stop();
            imageAnimatorProperty.reset();
        }
        loading.setVisibility(Component.HIDE);

    }

    public static void showToast(Context context, String toastText) {
        Text text = new Text(context);
        text.setText(toastText);
        text.setPadding(10, 10, 10, 10);
        ComponentContainer.LayoutConfig layoutConfig = new ComponentContainer.LayoutConfig(
                ComponentContainer.LayoutConfig.MATCH_CONTENT,
                ComponentContainer.LayoutConfig.MATCH_CONTENT
        );
        text.setLayoutConfig(layoutConfig);
        text.setTextColor(Color.BLACK);
        text.setTextSize(16, Text.TextSizeType.FP);
        new ToastDialog(context).setComponent(text).show();
    }
}
